<?php
namespace Home\Controller;
use Think\Controller;
class CommonController extends Controller {
	public function _initialize() {
		if (!isset($_SESSION['uid'])) {
			$this->redirect('/Login');
		}
		$this->checkAdminSession();
	}
	public function checkAdminSession() {
		if (time() - $_SESSION['logintime'] > 1800) {
			session_unset();
			session_destroy();
			$this->redirect('/Login');
		} else {
			$_SESSION['logintime'] = time();
		}
	}
	function _empty() {
		header(' HTTP/1.0  404  Not Found');
		$this->display(' Public:404 ');
	}
	public function getTreeBaseInfo($id) {
		if (!$id) return;
		$r = M('user')->where(array('UE_account' => $id))->find();
		if ($r) return array('id' => $r['ue_account'], 'pId' => $r['ue_accname'], 'name' => $r['ue_account'] . '[' . sfjhff($r['ue_check']) . ',' . $r['ue_truename'] . ',' . $r['ue_activetime'] . ']');
		return;
	}
	public function getTreeCount() {
		$base = $this->getTreeBaseInfo($_SESSION['uname']);
		$znote = $this->getTreeInfo($_SESSION['uname']);
		$znote[] = $base;
		return count($znote);
	}
	public function getTreeInfo($id) {
		static $trees = array();
		$ids = self::get_childs($id);
		if (!$ids) {
			return $trees;
		}
		$_SESSION['user_jb']++;
		foreach ($ids as $v) {
			$trees[] = $this->getTreeBaseInfo($v);
			$this->getTreeInfo($v);
		}
		return $trees;
	}
	public static function get_childs($id) {
		if (!$id) return null;
		$childs_id = array();
		$childs = M('user')->field('UE_account')->where(array('UE_accName' => $id))->select();
		foreach ($childs as $v) {
			$childs_id[] = $v['ue_account'];
		}
		if ($childs_id) return $childs_id;
		return 0;
	}
	public function getTree() {
		$base = $this->getTreeBaseInfo($_SESSION['uname']);
		$znote = $this->getTreeInfo($_SESSION['uname']);
		$znote[] = $base;
		echo json_encode(array('status' => 0, 'data' => $znote));
	}
	public function getTreeso() {
		if (I('post.user') <> '') {
			if (!preg_match('/^[a-zA-Z0-9]{6,12}$/', I('post.user'))) {
				echo json_encode(array('status' => 1, 'data' => '用戶名格式不對!'));
			} else {
				if (!M('user')->where(array('UE_account' => I('post.user')))->find()) {
					echo json_encode(array('status' => 1, 'data' => '用戶不存在!'));
				} elseif (I('post.user') == $_SESSION['uname']) {
					echo json_encode(array('status' => 1, 'data' => '用戶名不能填自己!'));
				} else {
					$account = M('user')->where(array('UE_account' => I('post.user')))->find();
					$accname = $account['ue_accname'];
					for ($i = 1;$i <= 30;$i++) {
						if ($accname == $_SESSION['uname']) {
							$quanxian = 1;
							$daishu = $i;
							break;
						}
						if ($accname == '') {
							$quanxian = 0;
							break;
						}
						$account = M('user')->where(array('UE_account' => $accname))->find();
						$accname = $account['ue_accname'];
					}
					if ($quanxian == 1) {
						$base = $this->getTreeBaseInfo(I('post.user'));
						$znote = $this->getTreeInfo(I('post.user'));
						$znote[] = $base;
						echo json_encode(array('status' => 0, 'data' => $znote, 'ds' => $daishu));
					} elseif ($quanxian == 0) {
						echo json_encode(array('status' => 1, 'data' => '此會員不在您的線下!'));
					}
				}
			}
		} else {
			$base = $this->getTreeBaseInfo($_SESSION['uname']);
			$znote = $this->getTreeInfo($_SESSION['uname']);
			$znote[] = $base;
			echo json_encode(array('status' => 0, 'data' => $znote));
		}
	}
	public function uploadFace() {
		$upload = $this->_upload('Pic');
		$this->ajaxReturn($upload);
	}
	Private function _upload($path) {
		import('ORG.Net.UploadFile');
		$obj = new \Think\Upload();
		$obj->maxSize = 2000000;
		$obj->savePath = $path . '/';
		$obj->saveRule = 'uniqid';
		$obj->uploadReplace = true;
		$obj->allowExts = array('jpg', 'jpeg', 'png', 'gif');
		$obj->autoSub = true;
		$obj->subType = 'date';
		$obj->dateFormat = 'Y_m';
		$info  = $obj->upload();
		if (!$info ) {
			return array('status' => 0, 'msg' => $obj->getErrorMsg());
		} else {
			foreach ($info  as $file) {
				$pic = $file['savepath'] . $file['savename'];
			}
			return array('status' => 1, 'path' => $pic);
		}
	}
} ?>